package main

import (
	"encoding/json"
	"fmt"
)

type Group struct {
	Children []string `json:"children"`
	//Hosts map[string][]string
}

/*
type Host struct {
	Hosts []string `json:"hosts"`
}
*/
type Vars struct {
	Variables map[string]string `json:"vars"`
}

type Inv struct {
	All struct {
		Hosts []string `json:"hosts"`
		//Host_vars map[string]string `json:"vars"`
		/*
			Vars struct {
				TaskId string `json:"task_id"`
			} `json:"vars"`
		*/
		Vars map[string]string `json:"vars"`
	} `json:"all"`
}

func main() {
	jsonstr := `{
		"all": {
		  "hosts": [
			"10.0.0.34"
		  ],
		  "vars": {
			"task_instance": "MGUserServer07",
			"task_role": "ansible_prepare_UserServer07",
			"task_action": "prepare",
			"ftpinfo": "ftp://DevOps:D3j8x6hJ@10.0.0.225/gok/",
			"JarVersion": "0.8.27.23.0619",
			"jarPath": "server/",
			"jarFilename": "MGUserServer.zip",
			"LinuxServerPath": "/data/gok/",
			"StartupOptions": "-Xms3072m -Xmx3072m -Xss4m -Xmn128m -XX:SurvivorRatio=1 -XX:PermSize=32m -XX:MaxPermSize=32m -XX:MaxTenuringThreshold=2 -XX:+UseParNewGC -XX:ParallelGCThreads=5 -XX:+UseConcMarkSweepGC -XX:ConcGCThreads=3 -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:CMSFullGCsBeforeCompaction=0 -XX:CMSInitiatingOccupancyFraction=70 -XX:+UseCMSInitiatingOccupancyOnly -XX:+CMSScavengeBeforeRemark -XX:+ExplicitGCInvokesConcurrent -XX:+PrintClassHistogram -XX:+PrintGC -XX:+PrintGCDetails -XX:+PrintGCTimeStamps  -XX:+UseAdaptiveSizePolicy -XX:GCTimeRatio=9 -Xnoclassgc -XX:+DisableExplicitGC -XX:SoftRefLRUPolicyMSPerMB=0",
			"SendThreadNum": "1",
			"RecSocketBufLen": "131072",
			"ServerTag": "WCGUserServer",
			"SynTaskDealThreadNum": "4",
			"ServerSendBackDealThreadNum": "1",
			"TimerCheckTime": "20",
			"TimerCheckAreaSize": "5000",
			"CheckMutex": "true",
			"LogLevel": "INFO",
			"RecBufferLen": "102400",
			"Port": "6707",
			"PlatServerIp": "127.0.0.1",
			"PlatServerPort": "6660",
			"PlatServerPassword": "wcg_basic_plat_server",
			"InternalIp": "127.0.0.1",
			"InternalPort": "6707",
			"areaTag": "common",
			"typeId": "1",
			"TimeZone": "GMT+8:00",
			"DCLogClose": "false",
			"UserDBHost": "127.0.0.1",
			"UserDBName": "MG_User",
			"UserDBUser": "wcggame",
			"UserDBPass": "wcggame",
			"UserDBTag": "main",
			"LogDBHost": "127.0.0.1",
			"LogDBName": "MG_US_Log",
			"LogDBUser": "wcggame",
			"LogDBPass": "wcggame",
			"LogDBTag": "log",
			"refPath": "..\\/__dlserverref",
			"task_id": "1101",
			"response_url": "http://operation28.mengjia.com/admin.php/Receive/getinfo/task_id/465/device_id/209/queueid/1101"
		  }
		}
	  }`

	/* "tasks": {"task_role":"preuserserver","task_id":100,"role_type_id": 1000} */
	type Hostvars struct {
		Host_vars map[string]map[string]string `json:"hostvars"`
	}
	/*
		type GlobalCustomVars struct {
			CustomVars map[stirng]string
		}
	*/
	// I don't *think* Ansible supports anything else in host groups, so it makes
	// sense to define it as a struct.

	type Group struct {
		Hosts    []string `json:"hosts"`
		Children []string `json:"children"`
		//Hosts map[string][]string
	}

	/*

		type Group struct {
			/Hosts HostList `json:"all"`
			Hosts map[string]HostList `json:"all"`
		}
	*/
	type Ansible struct {
		Metadata Hostvars `json:"_meta"`
		//Group    map[string]map[string]Group
		HostsGroup  Group `json:"all"`
		HostsGroup1 Group `json:"sandbox"`
		//HostsGroup Group
	}
	/*
		type Ansible struct {
			Metadata  Hostvars `json:"_meta"`
			HostGroup Group `json:"all"`
		}
	*/
	//fmt.Println(jsonstr)

	var data Inv
	var new_data map[string]string
	fmt.Println("enter struct")
	if err := json.Unmarshal([]byte(jsonstr), &data); err == nil {
		fmt.Println("struct ansible inventory")
		//fmt.Println(data.All.Host_vars)
		//fmt.Println(data.All.Vars.TaskId)
		key := "ftpinfo"
		new_data = make(map[string]string)
		if _, ok := data.All.Vars[key]; ok {
			fmt.Println(data.All.Vars[key])
			new_data[key] = data.All.Vars[key]
			fmt.Println(ok)
		} else {
			fmt.Println("could not get ftpinfo")
		}
		//fmt.Println(data)
	} else {
		fmt.Println(err)
	}

	//测试map查找key以及取值和重新赋值
	new_data["aa"] = "bb"
	fmt.Println("staring list new_data")
	fmt.Println(new_data)
	for key, value := range new_data {
		fmt.Println(key, value)
	}
	/*
		m, ok := v.Inv["all"]
		if !ok {
			return
		}

			fmt.Println(reflect.TypeOf(m))
			mStr := m.(string)
			fmt.Println(mStr)
	*/
}
